﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace HYRElectric.Dimension
{
	public class WireDimension
	{
		public static void DeleteTagOfElement(Document doc, Wire wire, string param)
		{
			foreach (Element element in new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(IndependentTag)).OfCategory(BuiltInCategory.OST_WireTags).ToElements())
			{
				IndependentTag independentTag = element as IndependentTag;
				if (independentTag.TaggedLocalElementId == wire.Id && (doc.GetElementById(independentTag.GetTypeId()) as FamilySymbol).GetParameter("标记类别").AsString() == param)
				{
					doc.Delete(independentTag.Id);
					doc.Regenerate();
					break;
				}
			}
		}

		public static void DeleteLeadLineGroups(Document doc, Element ele, string paraName)
		{
			string text = ele.GetParameter(paraName).AsString();
			if (!string.IsNullOrEmpty(text))
			{
				new List<Element>();
				foreach (Element element in new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_IOSDetailGroups).ToElements().ToList<Element>())
				{
					if (element.Id.ToString() == text)
					{
						doc.DeleteElement(element);
					}
				}
			}
		}

		public static ElementId CreatLeadLine(UIDocument uiDoc, Element elem, XYZ sPoint, XYZ pickPoint, XYZ hVector, double tagLength, bool isAddLeadHead)
		{
			Document document = uiDoc.Document;
			View activeView = document.ActiveView;
			ElementSet elementSet = new ElementSet();
			if (isAddLeadHead)
			{
				DetailLine detailLine = WireDimension.CreateLeadHead(document, activeView, elem, sPoint, pickPoint);
				elementSet.Insert(detailLine);
			}
			Curve curve = YJKLineEx.YJKGetBound(sPoint, pickPoint);
			DetailLine detailLine2 = document.Create.NewDetailCurve(activeView, curve) as DetailLine;
			elementSet.Insert(detailLine2);
			curve = YJKLineEx.YJKGetBound(pickPoint, pickPoint + tagLength * hVector);
			detailLine2 = (document.Create.NewDetailCurve(activeView, curve) as DetailLine);
			elementSet.Insert(detailLine2);
			return document.CreatYJKGroup(elementSet).Id;
		}

		public static DetailLine CreateLeadHead(Document doc, Autodesk.Revit.DB.View view, Element element, XYZ sPoint, XYZ pickPoint)
		{
			double num = 0.5;
			XYZ xyz = new XYZ(1.0, 0.0, 0.0);
			LocationCurve locationCurve = element.Location as LocationCurve;
			XYZ hyendPoint = locationCurve.Curve.GetEndPoint(0);
			XYZ hyendPoint2 = locationCurve.Curve.GetEndPoint(1);
			XYZ xyz2 = (hyendPoint + hyendPoint2) / 2.0;
			XYZ xyz3;
			if (hyendPoint2.X > hyendPoint.X)
			{
				xyz3 = new XYZ(hyendPoint2.X, hyendPoint2.Y, sPoint.Z) - new XYZ(hyendPoint.X, hyendPoint.Y, sPoint.Z);
			}
			else
			{
				xyz3 = new XYZ(hyendPoint.X, hyendPoint.Y, sPoint.Z) - new XYZ(hyendPoint2.X, hyendPoint2.Y, sPoint.Z);
			}
			double num2 = xyz.AngleTo(xyz3);
			Curve curve = YJKLineEx.YJKGetBound(sPoint, pickPoint);
			DetailLine detailLine = doc.Create.NewDetailCurve(view, curve) as DetailLine;
			curve = YJKLineEx.YJKGetBound(new XYZ(hyendPoint.X, hyendPoint.Y, sPoint.Z), new XYZ(hyendPoint2.X, hyendPoint2.Y, sPoint.Z));
			DetailLine detailLine2 = doc.Create.NewDetailCurve(view, curve) as DetailLine;
			Line line = YJKLineEx.YJKGetBound(new XYZ(xyz2.X, xyz2.Y, sPoint.Z), new XYZ(xyz2.X, xyz2.Y, 10.0));
			ElementTransformUtils.RotateElement(doc, detailLine.Id, line, num2);
			ElementTransformUtils.RotateElement(doc, detailLine2.Id, line, num2);
			XYZ hyendPoint3 = (detailLine.Location as LocationCurve).Curve.GetEndPoint(0);
			XYZ hyendPoint4 = (detailLine.Location as LocationCurve).Curve.GetEndPoint(1);
			Curve curve2 = (detailLine2.Location as LocationCurve).Curve;
			XYZ xyz4;
			if (curve2.GetEndPoint(1).X > curve2.GetEndPoint(0).X)
			{
				xyz4 = (curve2.GetEndPoint(0) - curve2.GetEndPoint(1)).Normalize();
			}
			else
			{
				xyz4 = (curve2.GetEndPoint(1) - curve2.GetEndPoint(0)).Normalize();
			}
			XYZ xyz5;
			if ((hyendPoint4.X > hyendPoint3.X && hyendPoint4.Y > hyendPoint3.Y) || (hyendPoint4.X < hyendPoint3.X && hyendPoint4.Y < hyendPoint3.Y))
			{
				xyz5 = new XYZ(0.0, 0.0, 1.0).CrossProduct(xyz4);
			}
			else
			{
				xyz5 = new XYZ(0.0, 0.0, -1.0).CrossProduct(xyz4);
			}
			XYZ xyz6 = hyendPoint3 - (xyz4 - xyz5).Normalize() * num;
			XYZ xyz7 = hyendPoint3 - (xyz5 - xyz4).Normalize() * num;
			Curve curve3 = YJKLineEx.YJKGetBound(new XYZ(xyz6.X, xyz6.Y, sPoint.Z), new XYZ(xyz7.X, xyz7.Y, sPoint.Z));
			DetailLine detailLine3 = doc.Create.NewDetailCurve(view, curve3) as DetailLine;
			ElementTransformUtils.RotateElement(doc, detailLine3.Id, line, -num2);
			ElementSet elementSet = new ElementSet();
			elementSet.Insert(detailLine);
			elementSet.Insert(detailLine2);
			foreach (object obj in elementSet)
			{
				Element elem = (Element)obj;
				doc.DeleteElement(elem);
			}
			return detailLine3;
		}
	}
}
